package com.two.screen.water.balance.ui.activity

import android.os.Bundle
import android.view.KeyEvent
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.two.screen.water.balance.R
import com.two.screen.water.balance.listener.MyOnPageChangeListener
import com.two.screen.water.balance.listener.MyOnTabSelectListener
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.ui.fragment.base.BaseFragment
import com.two.screen.water.balance.ui.fragment.login.LoginPwdFragment
import com.two.screen.water.balance.ui.fragment.login.LoginScanQrFragment
import com.two.screen.water.balance.ui.fragment.login.LoginSwipingCardFragment
import com.two.screen.water.balance.utils.ARouterUtils
import com.two.screen.water.balance.utils.eventbus.Event
import com.two.screen.water.balance.utils.eventbus.EventCode.Companion.LOGIN_SUCCESS_BUYER
import com.two.screen.water.balance.utils.eventbus.EventCode.Companion.LOGIN_SUCCESS_SELLER
import kotlinx.android.synthetic.main.activity_login24.*


@Route(path = ARouterUtils.LoginActivity24)
class LoginActivity24 : BaseActivity() {
    val TAG = LoginActivity24::class.java.simpleName.plus("=>")

    override fun getContentLayout(): Any = R.layout.activity_login24
    @JvmField @Autowired var mIdType = 0 //1：是卖家;2：是买家

    companion object {
        val LOGIN_ID_TYPE_SELLER = 1
        val LOGIN_ID_TYPE_BUYER = 2
    }

    private lateinit var mFragments: MutableList<BaseFragment>
    private var mTitles: MutableList<String> = mutableListOf("刷卡登录", "扫码登录", "密码登录")
    private var mMyPagerAdapter: MyPagerAdapter? = null
    var mCurFragment: BaseFragment? = null


    override fun initView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        mFragments = mutableListOf(LoginSwipingCardFragment(mIdType), LoginScanQrFragment(mIdType), LoginPwdFragment(mIdType))
    }

    override fun initData() {
        mLoginNameTv.text = when (mIdType) {
            LOGIN_ID_TYPE_SELLER -> "卖家登录"
            LOGIN_ID_TYPE_BUYER -> "买家登录"
            else -> "登陆"
        }
        initVp()
    }

    private fun initVp() {
        mMyPagerAdapter = MyPagerAdapter(supportFragmentManager)
        mVp.adapter = mMyPagerAdapter
        mStl.setViewPager(mVp)
        mVp.addOnPageChangeListener(object : MyOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                mCurFragment = mFragments?.get(position)
            }
        })
        mStl.setOnTabSelectListener(object : MyOnTabSelectListener() {
            override fun onTabSelect(position: Int) {
                mCurFragment = mFragments?.get(position)
            }
        })
        mStl.currentTab = 0
        mVp.currentItem = 0
        mCurFragment = mFragments.get(0)
    }

    override fun initListener() {
        mGoBackLayout.setOnClickListener {
            finish()
        }
    }

    //菜品适配器
    inner class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment = mFragments.get(position)

        override fun getCount(): Int = mFragments.size

        override fun getPageTitle(position: Int): CharSequence = mTitles.get(position)
    }

    override fun isRegisterEventBus(): Boolean = true
    override fun receiveEvent(event: Event<Any>) {
        super.receiveEvent(event)
        when (event.code) {
            LOGIN_SUCCESS_SELLER -> {
                finish()
            }
            LOGIN_SUCCESS_BUYER -> {
                finish()
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (event?.device?.name?.contains("USB", true) != true) return false
        if (event.action == KeyEvent.ACTION_DOWN) {//处理触发两次
            when (keyCode) {
                33 -> { //d("帐户/E")
                    mGoBackLayout.performClick()
                    return true
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}